// pages/consignmentManagement/consignmentManagement.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    date: '',
    show: false,
    status_list: ["派送中", "已揽件"],
    path_list: [], // 路线选择
    path_index: -1,
    payment_index: -1,
    person_index: -1,
    list: [],
    minDate: new Date(2020,0,1).getTime(),
    payment_method: [{
      title: "现金",
      id: 1
    },
    {
      title: "到付",
      id: 5
    }
    ],  // 付款方式
    pullPerson: [], // 揽件人
    height: 0,
    startTime: 0,
    endTime: 0,
    name: '',
    pickerConfig: {                     // 时间筛选
      endDate: true,
      dateLimit: false,
      initStartTime: "2019-01-01",
      initEndTime: "2019-12-01",
      limitStartTime: "2000-01-01",
      limitEndTime: "2100-12-31"
    },
    company_type: 0                 //企业类型；0：物流、1：快递
  },
  // 路线选择
  bindPickerChange(e) {
    console.log("路线", e)
    let {
      value
    } = e.detail || {};
    let data = {}
    data[e.currentTarget.dataset.type] = value
    console.log(data)
    this.setData(data)
  },
  onDisplay() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    date = [new Date(date.startTime), new Date(date.endTime)];
    return `${date[0].getFullYear()}/${date[0].getMonth() + 1}/${date[0].getDate()}-${date[1].getFullYear()}/${date[1].getMonth() + 1}/${date[1].getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
      startTime: parseInt(new Date(event.detail.startTime).getTime()/1000),
     endTime: parseInt(new Date(event.detail.endTime).getTime()/1000)
    });
  },
  // 去详情
  toDetail(e) {
    wx.navigateTo({
      url: '/pages/consignmentDetail/consignmentDetail?id=' + e.currentTarget.dataset.id,
    })
  },

  changeName(e){
    this.setData({
      name: e.detail
    })
  },

  // 获取托运单列表
  getData(data) {
    wx.$api.listLogistics(data || {}).then(res => {
      if (res.data.code == 200) {
        this.setData({
          list: res.data.data.list
        })
      }
    })
  },

  searchData(){
    this.setData({
      list: []
    })
    let data = {
       member_name: this.data.name,
       create_time_start: this.data.startTime,
       create_time_end:this.data.endTime,
       tansline_id:this.data.path_index == -1 ? '' : this.data.path_list[this.data.path_index].tansline_id,
       employee_id:this.data.person_index == -1 ? '' : this.data.pullPerson[this.data.person_index].employee_id ,
       pay_type: this.data.payment_index == -1 ? '' : this.data.payment_method[this.data.payment_index].id
    }

    this.getData(data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let query = wx.createSelectorQuery(), that = this, height = wx.getStorageSync('SystemInfo')['screenHeight'];
    console.log("设备信息", wx.getStorageSync('SystemInfo'))
    console.log("元素高度", height)
    query.select('.search').boundingClientRect(function (rect) {
      that.setData({
        height: height - rect.height + 'px',
        company_type: wx.getStorageSync('user_infos').company_type
      })
      console.log(that.data.height, 'mmmmmmmm')
    }).exec();

    this.setData({
      path_list: wx.getStorageSync('lineList')
    })
    this.getData();

    wx.$api.getRegionList().then(res => {
      if (res.data.code)
        this.setData({
          path_list: res.data.data.list
        })
    })

    wx.$api.allEmployee().then(res => {
      if (res.data.code) {
        this.setData({
          pullPerson: res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})